package com.tzxx.spider.utils;

import lombok.extern.slf4j.Slf4j;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author Created by tzxx
 * @since : 2019/3/18.
 */
@Slf4j
public class HtmlUtils {

    public static String html2Text(String inputString) {
        //含html标签的字符串
        String htmlStr = inputString;
        String textStr = "";
        Pattern scriptPattern;
        Matcher scriptMatcher;
        Pattern stylePattern;
        Matcher styleMatcher;
        Pattern htmlPattern;
        Matcher htmlMatcher;

        try {
            //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
            String regExScript = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
            //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
            String regExStyle = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
            //定义HTML标签的正则表达式
            String regExHtml = "<[^>]+>";

            scriptPattern = Pattern.compile(regExScript, Pattern.CASE_INSENSITIVE);
            scriptMatcher = scriptPattern.matcher(htmlStr);
            //过滤script标签
            htmlStr = scriptMatcher.replaceAll("");

            stylePattern = Pattern.compile(regExStyle, Pattern.CASE_INSENSITIVE);
            styleMatcher = stylePattern.matcher(htmlStr);
            //过滤style标签
            htmlStr = styleMatcher.replaceAll("");

            htmlPattern = Pattern.compile(regExHtml, Pattern.CASE_INSENSITIVE);
            htmlMatcher = htmlPattern.matcher(htmlStr);
            //过滤html标签
            htmlStr = htmlMatcher.replaceAll("");
            htmlStr = htmlStr.replace("  ", "");
            htmlStr = htmlStr.replaceAll("&nbsp;", "");
            htmlStr = htmlStr.replace("\n", "");
            htmlStr = htmlStr.replace("\t", "");
            textStr = htmlStr.trim();
        } catch (Exception e) {
            return inputString;
        }
        //返回文本字符串
        return textStr;
    }

    public static List<String> getHtmlImg(String html){
        List<String> links = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            links.add(element.getAllElements().attr("src").replaceAll("\\\\\"",""));
        }
        return links;
    }

    /**
     * 处理url中的相对路径或者绝对路径（不带domain的）
     *
     * @param currentLink 当前页url
     * @param url 需要处理的url
     * @return
     */
    public static String handleUrl(String currentLink, String url){
        if (url.startsWith("./")) {
            //拼绝对路径
            url = url.replaceAll("\\./", "");
            url = currentLink.substring(0, currentLink.lastIndexOf('/') + 1) + url;
        } else {
            //拼相对路径
            URL url1 = null;
            try {
                url1 = new URL(currentLink);
            } catch (MalformedURLException e) {
                log.error("erro:{}",e.getMessage());
                return "";
            }
            url = url1.getHost() + currentLink.substring(0, currentLink.lastIndexOf('/'));
        }
        return url;
    }
}
